package cn.com.ghy.android.widget.coopvisit;

import java.util.List;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TabWidget;
import android.widget.TextView;
import cn.com.ghy.android.bean.CoopVisitDealerInfoBean;
import cn.com.ghy.android.bean.CoopVisitInfoBean;
import cn.com.ghy.android.bean.LoginInfoBean;
import cn.com.ghy.android.exception.DisposeException;
import cn.com.ghy.android.service.VsPartOpeService;
import cn.com.ghy.android.util.CommonStatic;
import cn.com.ghy.android.widget.ComStaticApp;
import cn.com.ghy.android.widget.R;

/**
 * @软件著作权:金红叶纸业集团有限公司
 * @系统名称:SFA系统
 * @Description:经销商信息填写
 * @author wuz  @date 2011-6-21 15:24:15
 * @版本历史:v1.0
 * @Copyright:Copyright (c) 2011-2012 
 */

public class CoopVisitDealer extends TabActivity {

	private TextView labTxtJxsId = null;
	private TextView labTxtJxsName = null;

	private EditText txtContent = null;
	private EditText txtQuestion = null;
	private EditText txtImprove = null;
	private EditText txtThreeavgmonthsale = null;
	private EditText txtMonthtarsale = null;
	private EditText txtTonowsale = null;
	private EditText txtTonowreach = null;
	private EditText txtLaststocknum = null;
	private EditText txtMonthzdays = null;
	private EditText txtWeekstocknum = null;

	private Button btnSubmit = null;
	private Button btnSubmitTwo = null;

	private ProgressDialog progressDialog = null;

	private Bundle bundle = null;
	private CoopVisitInfoBean coopVisitInfo = null;
	private CoopVisitDealerInfoBean coopVisitDealerInfo = null;
	private List<CoopVisitDealerInfoBean> list = null;
	private LoginInfoBean loginInfo = null;
	private static final String VSTYPE = CommonStatic.VSTYPE_JXS;

	private HttpThread httpThread = null;

	private DisposeException disposeException = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.coop_visit_dealer);
		getSession();
		TabHost tabHost = getTabHost();
		TabSpec tab1 = tabHost.newTabSpec("tab1");
		tab1.setIndicator("基本信息");
		tab1.setContent(R.id.linBaseInfo);
		tabHost.addTab(tab1);

		TabSpec tab2 = tabHost.newTabSpec("tab2");
		tab2.setIndicator("销量和库存了解");
		tab2.setContent(R.id.linInfo);
		tabHost.addTab(tab2);

		tabHost.setCurrentTab(0);

		TabWidget tabWidget = tabHost.getTabWidget();
		for (int i = 0; i < tabWidget.getChildCount(); i++) {
			tabWidget.getChildAt(i).getLayoutParams().height = getWindowManager()
					.getDefaultDisplay().getHeight() / 12;
		}
		findView();
		init();
		addListener();
	}

	@SuppressWarnings("unchecked")
	private void getSession() {
		disposeException = ((ComStaticApp) getApplication()).getDisposeException();
		loginInfo = ((ComStaticApp) getApplication()).getLoginInfo();
		bundle = getIntent().getExtras();
		coopVisitInfo = (CoopVisitInfoBean) bundle.getSerializable("coopVisitInfo");
		list = (List<CoopVisitDealerInfoBean>) bundle.getSerializable("coopVisitDealerInfoList");
	}

	private void findView() {
		labTxtJxsId = (TextView) findViewById(R.id.labTxtJxsId);
		labTxtJxsName = (TextView) findViewById(R.id.labTxtJxsName);

		txtContent = (EditText) findViewById(R.id.txtContent);
		txtQuestion = (EditText) findViewById(R.id.txtQuestion);
		txtImprove = (EditText) findViewById(R.id.txtImprove);
		txtThreeavgmonthsale = (EditText) findViewById(R.id.txtThreeavgmonthsale);
		txtMonthtarsale = (EditText) findViewById(R.id.txtMonthtarsale);
		txtTonowsale = (EditText) findViewById(R.id.txtTonowsale);
		txtTonowreach = (EditText) findViewById(R.id.txtTonowreach);
		txtLaststocknum = (EditText) findViewById(R.id.txtLaststocknum);
		txtMonthzdays = (EditText) findViewById(R.id.txtMonthzdays);
		txtWeekstocknum = (EditText) findViewById(R.id.txtWeekstocknum);

		btnSubmit = (Button) findViewById(R.id.btnSubmit);
		btnSubmitTwo = (Button) findViewById(R.id.btnSubmitTwo);
	}

	private void init() {
		labTxtJxsId.setText(coopVisitInfo.getKunnr());
		labTxtJxsName.setText(coopVisitInfo.getKunnrname());
		if (null != list && list.size() > 0) {
			coopVisitDealerInfo = list.get(0);
			txtContent.setText(null == coopVisitDealerInfo.getContent() ? "" : coopVisitDealerInfo
					.getContent());
			txtQuestion.setText(null == coopVisitDealerInfo.getQuestion() ? ""
					: coopVisitDealerInfo.getQuestion());
			txtImprove.setText(null == coopVisitDealerInfo.getImprove() ? "" : coopVisitDealerInfo
					.getImprove());
			txtThreeavgmonthsale.setText(null == coopVisitDealerInfo.getThreeavgmonthsale() ? ""
					: coopVisitDealerInfo.getThreeavgmonthsale());
			txtMonthtarsale.setText(null == coopVisitDealerInfo.getMonthtarsale() ? ""
					: coopVisitDealerInfo.getMonthtarsale());
			txtTonowsale.setText(null == coopVisitDealerInfo.getTonowsale() ? ""
					: coopVisitDealerInfo.getTonowsale());
			txtTonowreach.setText(null == coopVisitDealerInfo.getTonowreach() ? ""
					: coopVisitDealerInfo.getTonowreach());
			txtLaststocknum.setText(null == coopVisitDealerInfo.getLaststocknum() ? ""
					: coopVisitDealerInfo.getLaststocknum());
			txtMonthzdays.setText(null == coopVisitDealerInfo.getMonthzdays() ? ""
					: coopVisitDealerInfo.getMonthzdays());
			txtWeekstocknum.setText(null == coopVisitDealerInfo.getWeekstocknum() ? ""
					: coopVisitDealerInfo.getWeekstocknum());
		} else {
			coopVisitDealerInfo = new CoopVisitDealerInfoBean();
		}
	}

	private void addListener() {

		btnSubmit.setOnClickListener(btnListner);
		btnSubmitTwo.setOnClickListener(btnListner);
	}

	private void getDate() {
		coopVisitDealerInfo.setKunnr(labTxtJxsId.getText().toString().trim());
		coopVisitDealerInfo.setVsdate(loginInfo.getNowdate().substring(0, 10));
		coopVisitDealerInfo.setContent(txtContent.getText().toString().trim());
		coopVisitDealerInfo.setQuestion(txtQuestion.getText().toString().trim());
		coopVisitDealerInfo.setImprove(txtImprove.getText().toString().trim());
		coopVisitDealerInfo.setThreeavgmonthsale(txtThreeavgmonthsale.getText().toString().trim());
		coopVisitDealerInfo.setMonthtarsale(txtMonthtarsale.getText().toString().trim());
		coopVisitDealerInfo.setTonowsale(txtTonowsale.getText().toString().trim());
		coopVisitDealerInfo.setTonowreach(txtTonowreach.getText().toString().trim());
		coopVisitDealerInfo.setLaststocknum(txtLaststocknum.getText().toString().trim());
		coopVisitDealerInfo.setMonthzdays(txtMonthzdays.getText().toString().trim());
		coopVisitDealerInfo.setWeekstocknum(txtWeekstocknum.getText().toString().trim());
		coopVisitDealerInfo.setCuser(loginInfo.getPernr());
	}

	private View.OnClickListener btnListner = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			getDate();
			httpThread = new HttpThread(handler);
			httpThread.doStart();
		}
	};

	private class HttpThread extends Thread {
		private Handler handle = null;

		public HttpThread(Handler hander) {
			handle = hander;
		}

		public void doStart() {
			progressDialog = new ProgressDialog(CoopVisitDealer.this);
			progressDialog.setTitle("提示信息");
			progressDialog.setMessage("正在请求数据，请稍等......");
			progressDialog.setCancelable(false);
			progressDialog.show();
			this.start();
		}

		public void run() {
			Message message = handle.obtainMessage();
			;
			bundle = new Bundle();
			String result = null;
			VsPartOpeService service = new VsPartOpeService();
			try {
				result = service.SendVsPartData(VSTYPE, coopVisitDealerInfo);
				progressDialog.dismiss();
				bundle.putString("result", result);
				message.what = CommonStatic.STATE_NORMAL;
			} catch (Exception ex) {
				progressDialog.dismiss();
				disposeException.handleException(ex);
			}
			message.setData(bundle);
			handle.sendMessage(message);
		}
	}

	private Handler handler = new Handler() {
		public void handleMessage(Message m) {
			String result = m.getData().getString("result");
			switch (m.what) {
				case CommonStatic.STATE_NORMAL :
					if ("OK".equals(result)) {
						new AlertDialog.Builder(CoopVisitDealer.this).setTitle("提示信息")
								.setMessage("提交成功, 是否关闭此页面!")
								.setPositiveButton("确定", new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dlg, int sumthin) {
										CoopVisitDealer.this.finish();
									}
								}).setNeutralButton("取消", null).show();
					} else {
						new AlertDialog.Builder(CoopVisitDealer.this).setTitle("提示信息")
								.setMessage(result).setNeutralButton("关闭", null).show();
					}
					break;
			}
		}
	};

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			new AlertDialog.Builder(CoopVisitDealer.this).setTitle("提示信息").setMessage("确定退出?")
					.setPositiveButton("确定", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dlg, int sumthin) {
							CoopVisitDealer.this.finish();
						}
					}).setNeutralButton("取消", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dlg, int sumthin) {
						}
					}).show();
		}
		return true;
	}
}
